********************************************************************************
/**THIS FILE CREATES FIGURE 1 FOR 
“The Effects of Education on Financial Outcomes: Evidence from Kenya”**
 
 Additional packages needed: spmap shp2dt shp2dta mif2dta
 */

 
// Set your local directory here
cd ""

global dr = "input_data/"
global d0 = "output_data/"
global dtab = "tables_figures/"


* Create files for spmap
shp2dta using "$dr/KEN_adm2", database("$d0/KEadm") coordinates("$d0/KEcoord") genid(id)

/*MAKING MAPS */

*** 
* Start with county level geographic file *
* Manually enter county codes to merge with sub-regions in FinAccess data *
**
use "$d0/KEadm", clear

gen county = 101 if NAME_2 == "Nairobi"

replace county = 201 if NAME_2 == "Nyandarua"
replace county = 202 if NAME_2 == "Nyeri"
replace county = 203 if NAME_2 == "Kirinyaga"
replace county = 204 if NAME_2 == "Murang'a"
replace county = 205 if NAME_2 == "Kiambu"

replace county = 301 if NAME_2 == "Mombasa"
replace county = 302 if NAME_2 == "Kwale"
replace county = 303 if NAME_2 == "Kilifi"
replace county = 304 if NAME_2 == "Tana River"
replace county = 305 if NAME_2 == "Lamu"
replace county = 306 if NAME_2 == "Taita Taveta"

replace county = 401 if NAME_2 == "Marsabit"
replace county = 402 if NAME_2 == "Isiolo"
replace county = 403 if NAME_2 == "Meru"
replace county = 404 if NAME_2 == "Nithi"
replace county = 405 if NAME_2 == "Embu"
replace county = 406 if NAME_2 == "Kitui"
replace county = 407 if NAME_2 == "Machakos"
replace county = 408 if NAME_2 == "Makueni"

replace county = 501 if NAME_2 == "Garissa"
replace county = 502 if NAME_2 == "Wajir"
replace county = 503 if NAME_2 == "Mandera"

replace county = 601 if NAME_2 == "Siaya"
replace county = 602 if NAME_2 == "Kisumu"
replace county = 603 if NAME_2 == "Migori"
replace county = 604 if NAME_2 == "Homa Bay"
replace county = 605 if NAME_2 == "Kisii"
replace county = 606 if NAME_2 == "Nyamira"

replace county = 701 if NAME_2 == "Turkana"
replace county = 702 if NAME_2 == "West Pokot"
replace county = 703 if NAME_2 == "Samburu"
replace county = 704 if NAME_2 == "Trans-Nzoia"
replace county = 705 if NAME_2 == "Baringo"
replace county = 706 if NAME_2 == "Uasin Gishu"
replace county = 707 if NAME_2 == "Elgeyo-Marakwet"
replace county = 708 if NAME_2 == "Nandi"
replace county = 709 if NAME_2 == "Laikipia"
replace county = 710 if NAME_2 == "Nakuru"
replace county = 711 if NAME_2 == "Narok"
replace county = 713 if NAME_2 == "Kericho"
replace county = 714 if NAME_2 == "Bomet"

replace county = 801 if NAME_2 == "Kakamega"
replace county = 802 if NAME_2 == "Vihiga"
replace county = 803 if NAME_2 == "Bungoma"
replace county = 804 if NAME_2 == "Busia"

assert !mi(county)

* Get county to sub-region crosswalk from FinAccess file
preserve 
	use "$dr/FinAccess_2016_data_final_anonymized.dta", clear
	keep sub_region a2
	rename a2 county
	duplicates drop
	tempfile county_subregion_cross
	save `county_subregion_cross'
restore

merge m:1 county using `county_subregion_cross', nogen assert(2 3) keep(3) 

* Get intensity within each sub-region *
merge m:1 sub_region using "$d0/fpe_intensity.dta", nogen assert(3) ///
keepusing(never_attend)

bys sub_region: gen n = _n

* Create maps *
replace never_attend=round(never_attend,.001)
format never_attend %9.3f

spmap never_attend using "$d0/KEcoord", id(id) fcolor(Greys2) clmethod(un) legorder(hilo)
graph export "$dtab/figure1.eps", replace

cap log close



